// pages/my/my.js
const db = wx.cloud.database()
let openid = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    wx.cloud.callFunction({
      name: "login"
    }).then(res => {
      openid = res.result.userInfo.openId
      this._getDays()
      this._getUser()
      // console.log(this.data.openid)
    }).catch(err => {
      console.log(err)
    })
    
  },

  // 查询连续签到天数
  _getDays: async function () {
    db.collection('sign').where({
      _openid: openid
    }).orderBy("sign_time", "desc").limit(1).get().then(res => {
      if (res.data.length == 0) {
        this.setData({
          continueDays: 0
        })
      } else {
        this.setData({
          continueDays: res.data[0].continue_sign_days
        })
      }
    })

  },
  // 查询用户信息
  _getUser: async function () {
    db.collection('user').where({
      _id: openid
    }).get().then((res) => {
      this.setData({
        userInfo: res.data[0]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.enableRefresh) {
      this.onLoad()
    } else {
      this.enableRefresh = true
    }
  },
  enableRefresh: false,

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  btnSigned: function() {
    wx.navigateTo({
      url: 'signIn/signIn',
    })
  },
  showIncode: function() {
    wx.navigateTo({
      url: 'integral/integral',
    })
  },
  showCocode: function() {
    wx.navigateTo({
      url: 'collect/collect',
    })
  },
  showLiCode: function() {
    wx.navigateTo({
      url: 'like/like',
    })
  },
  showBlCode: function() {
    wx.navigateTo({
      url: 'blog/blog',
    })
  },
  showDdCode: function() {
    wx.navigateTo({
      url: 'dingdan/dingdan',
    })
  },

  // 去个人主页
  gotoperhome(value) {
    console.log(value)
    let openid = value.currentTarget.dataset.openid
    wx.navigateTo({
      url: `../perhome/perhome?openid=${openid}`,
    })
  }
})